package com.urguxxi.springdemo.controller;

import com.urguxxi.springdemo.service.LoginService;
import com.urguxxi.springdemo.dto.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.urguxxi.springdemo.entity.Admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {
    /**
     * 进入登录页面
     * @return
     */
    @Autowired
    private LoginService loginService;

    @GetMapping("/page")
    public String loginPage(){
        return "login";
    }


    @PostMapping("do")
    @ResponseBody
    public ResultDTO login(Admin admin, HttpSession session){
        //数据校验
        //获取所有错误
//        List<ObjectError> allErrors = bindingResult.getAllerror(){

        return loginService.login(admin,session);
    }

    //2021.6.6 布置作业，退出登录
    @GetMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return  "/login";
    }

}
